package com.assistant.generate;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

public class MybatisPlusGenerate {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://server2:3306/hongyu-edu", "root", "hongyu")
                .globalConfig(builder -> {
                    builder.author("auto-generate") // 设置作者
                            .commentDate("yyyy-MM-dd")
                            .disableOpenDir()
                            .outputDir(System.getProperty("user.dir")+"/struct-web/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }else if (typeCode == Types.BIGINT){
                        // 自定义类型转换
                        return DbColumnType.LONG;
                    }else if (typeCode == Types.CHAR){
                        // 自定义类型转换
                        return DbColumnType.BASE_CHAR;
                    }else if (typeCode == Types.TIMESTAMP){
                        // 自定义类型转换
                        return DbColumnType.DATE;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.yungee") // 设置父包名
                            .moduleName("learn") // 设置父包模块名
                            .entity("entity")
                            .controller("controller")
                            .service("service")
                            .serviceImpl("service.impl")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir")+"/assistant-web/src/main/resources/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("train_course_media") // 设置需要生成的表名
                            .entityBuilder().enableLombok()
                            .enableFileOverride()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .enableTableFieldAnnotation()
                            .mapperBuilder().superClass(BaseMapper.class)
                            .enableFileOverride()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .serviceBuilder()
                            .enableFileOverride()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .controllerBuilder().formatFileName("%sController")
                            .enableRestStyle()
                            .enableFileOverride()
                    ;

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
